import { RouteRecordRaw } from 'vue-router'
import { defineStore } from 'pinia'
import router from '@/router'
import { baseAddress, getMenuListByRoleId } from '@/api/url'
import { post, Response } from '@/api/http'
import { defaultRoutes } from '@/router/routes/default-routes'
import { findRootPathRoute, generatorRoutes, mapTwoLevelRouter } from '../help'
import { constantRoutes } from '@/router/routes/constants'
import MenuService from '@/service/system/menu'
import { transformToOriginRoutes, DataMapping } from '@/utils'

const usePermissionStore = defineStore('permission-route', {
  state: () => {
    return {
      permissionRoutes: [] as RouteRecordRaw[],
    }
  },
  getters: {
    getPermissionSideBar(state) {
      return state.permissionRoutes.filter((it) => {
        return it.meta && !it.meta.hidden
      })
    },
    getPermissionSplitTabs(state) {
      return state.permissionRoutes.filter((it) => {
        return it.meta && !it.meta.hidden && it.children && it.children.length > 0
      })
    },
    getTopLevelTabs(state) {
      return state.permissionRoutes
        .filter((it) => {
          return it.meta && !it.meta.hidden && it.children && it.children.length > 0
        })
        .map((it) => {
          const obj = { ...it, items: it.children }
          delete obj.children
          return obj
        })
    },
  },
  actions: {
    async getRoutes() {
      try {
        // 从后台获取所有资源数据
        const res: Response = await MenuService.getRoutes({})
        // 过滤掉不是系统和菜单的资源
        function filterMenuRoutes(menuList: any, parentPath: string = '') {
          return menuList.filter((it: any) => {
            it.parentPath = parentPath
            if (it.type !== 0 && it.type !== 1) {
              return false
            }
            if (it.children && it.children.length > 0) {
              const tempParentPath = parentPath ? parentPath + '/' + it.menuUrl : it.menuUrl
              const childrenList = filterMenuRoutes(it.children, tempParentPath)
              if (childrenList.length > 0) {
                it.children = childrenList
              } else {
                it.children = undefined
              }
            } else {
              it.children = undefined
            }
            return true
          })
        }
        const menuList = filterMenuRoutes(res.data)
        // 将数据转化成我们需要的格式
        const dataMapping: DataMapping = {
          menuUrl: 'menuUrl',
          menuName: 'name',
          icon: 'icon',
          hidden: 'isView',
          localFilePath: 'localFilePath',
          children: 'children',
          parentPath: 'parentPath',
          affix: 'affix',
          cacheable: 'cacheable'
        }
        let routes: any = [];
        if (import.meta.env.MODE === 'development') {
          routes = [...transformToOriginRoutes(dataMapping, menuList), ...defaultRoutes]
        } else {
          routes = [...transformToOriginRoutes(dataMapping, menuList)]
        }
        return generatorRoutes(routes)
      } catch (error) {
        console.log(error, 'error')
        console.log(
          '路由加载失败了，请清空一下Cookie和localStorage，重新登录；如果已经采用真实接口的，请确保菜单接口地址真实可用并且返回的数据格式和mock中的一样'
        )
        return []
      }
    },
    async initPermissionRoute() {
      // 加载路由
      const accessRoutes = await this.getRoutes()
      const mapRoutes = mapTwoLevelRouter(accessRoutes)
      mapRoutes.forEach((it: any) => {
        router.addRoute(it)
      })
      // 配置 `/` 路由的默认跳转地址
      router.addRoute({
        path: '/',
        redirect: findRootPathRoute(accessRoutes),
        meta: {
          hidden: true,
        },
      })
      // 这个路由一定要放在最后
      router.addRoute({
        path: '/:pathMatch(.*)*',
        redirect: '/404',
        meta: {
          hidden: true,
        },
      })
      this.permissionRoutes = [...constantRoutes, ...accessRoutes]
    },
    isEmptyPermissionRoute() {
      return !this.permissionRoutes || this.permissionRoutes.length === 0
    },
    reset() {
      this.$reset()
    },
  },
})

export default usePermissionStore
